<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1、ECMAScript基础(深入)</title>
</head>
<body>
    <script>
        // 链式操作
        var sched = {
            a: function(){
                console.log('a');
                return this;
            },
            b: function(){
                console.log('b');
                return this;
            }
        }
        sched.a().b();
        // 执行可以用.或[]
        sched['a']()['b']();

        // 枚举 => 遍历
        var car  = {
            brand: 'Banz',
            color: 'red'
        }
        for(var key in car){
            // console.log(car.key); car.key => car['key'] => undefined
            console.log(car[key]);
        }

        ;(function(){
            var obj  = {
                name: '艾小叶',
                age: 32
            }
            for(var key in obj){
                if(obj.hasOwnProperty(key)){
                    console.log(key + ':' + obj[key]);
                }
            }

            console.log('============分割线===========');
            // 如果是自定义对象，使用for...in...会打印所有的属性，包括原型和原型链上的
            function Car(brand, color){
                this.brand = brand;
                this.color = color;
            }
            Car.prototype = {
                lang: 5,
                width: 2
            }
            Object.prototype.name = 'Object';

            var car = new Car('Banz', '红色');
            
            for(var key in car){
                console.log(key + ':' + car[key]);
            }
            // 使用hasOwnProperty之后就只会打印自定义对象内部属性
            console.log('---------分割线---------');
            for(var key in car){
                if(car.hasOwnProperty(key)){
                    console.log(key + ':' + car[key]);
                }
            }
        })()
        console.log('------分割线-----');
        ;(function(){
            var car = {
                brand: 'Banz',
                color: 'red'
            }

            console.log('displacement' in car); // car是否存在该键
            // in 不但会查找自己内部，连原型和原型链都会去查找
        })()
        console.log('--------分割线------');
        ;(function(){
            function Car(){}
            var car = new Car();

            function Person(){}
            var p = new Person();

            console.log(car instanceof Car);
            console.log(car instanceof Object);
            console.log([] instanceof Array);
            console.log({} instanceof Object);
            console.log({} instanceof Object);

            // A对象的原型里到底有没有B的原型
        })()

        console.log('--------分割线--------');
        var a = [] || {}
        console.log(a.constructor);
        console.log(a instanceof Array);
        console.log(Object.prototype.toString.call(a));
    </script>
</body>
</html>